// pages/scoreOrder/scoreOrder.js
const orderService = require('../../api/orderService.js')

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    member: {},
    onLoad: true,
    page: 1,
    pageSize: 10,
    loadingText: '正在加载...',
    loading: false
  },
  getOrderList:function() {
    const that = this
    let orders = this.getOrderData(1, 10)
    Promise.all([orders]).then(res => {
      that.setData({
        orderList: res[0],
        onLoad: false
      })
    })
  },
  // 获取订单数据
  getOrderData:function(page, pageSize) {
    const that = this
    let postData = {
      page: page,
      pageSize: pageSize,
      memberIdEquals: that.data.member.id,
      businessEquals: 'SCORE'
    }
    let listData = orderService.query(postData).then(res => {
      console.log(res)
      let tempList = []
      if (res.records && res.records.length > 0) {
        res.records.forEach(item => {
          let tempItem = {
            orderNum: item.id,
            status: item.status,
            business: item.business,
            storeName: item.storeName, //发货门店名称
            storeId: item.storeId,
            shipmentAmount: item.shipmentAmount ? item.shipmentAmount : 0, //运费
            details: [],
            buyNumber: item.buyNumber ? item.buyNumber : 0,
            totalPrice: item.cashTotal,
            exchangeScores: item.images.length > 0 ? item.images[0].exchangeScores : ''
          }
          if (item.images) {
            item.images.forEach((i) => {
              tempItem.count += parseInt(i.goodsNum);
              tempItem.details.push({
                id: i.productId,
                img: i.url,
                goodsName: i.goodsName,
                goodsNum: i.productNum,
              });
            })
          } else {
            tempItem.count = 0
          }
          tempList.push(tempItem)
        });
      }
      return tempList
    }).catch(e => {
      return []
    })
    return listData
  },
  // 前往订单详情页
  toOrderDetails: function (val) {
    console.log(val)
    let orderId = val.currentTarget.dataset.id
    wx.navigateTo({
      url: '../orderDetails/orderDetails?orderId=' + orderId
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    if (app.globalData.userInfo) {
      console.log(app.globalData.userInfo)
      if (app.globalData.userInfo.member) {
        this.setData({
          member: app.globalData.userInfo.member
        })
        this.getOrderData(this.data.page)
      }
    }
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      loading: false
    })
    this.getOrderList()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      loading: true
    })
    const that = this
    const page = that.data.page
    const pageSize = that.data.pageSize
    let index = page
    index++
    let orders = this.getOrderData(index, pageSize)
    Promise.all([orders]).then(res => {
      let oldOrders = that.data.orderList
      if (res[0].length > 0) {
        let newOrders = oldOrders.concat(res[0])
        that.setData({
          orderList: newOrders,
          loading: false,
          page: index
        })
      } else {
        that.setData({
          loadingText: '没有更多数据'
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})